<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<#include "../head.html">
		<title>字典管理</title>
		<script type="text/javascript">
		var queryForm,queryDialog,inputForm,inputDialog,grid;
		$(function(){
			//查询
			queryForm = Rainbow.Form({
				  formId:"#queryForm"
				 ,lable:true
				 ,inputArray:[
					 {lable:"名称",name:"name",type:"textbox",condition:"like"}
					,{lable:"编码",name:"code",type:"textbox",condition:"like"}
					
				  ]
				 ,queryForm:true
			 });
			
			queryDialog = Rainbow.Dialog({
		  		  title:"查询条件"
				 ,dialogId:'#queryDialog'
				 ,form:queryForm
				 ,width: '80%'
			 	 ,height: '50%'
		 		 ,modal: false
			     ,closable:true
			 	 ,buttons:[{
						text:'确认',
						iconCls:'icon-ok',
						handler:function(){
							grid.query();
							queryDialog.dialog('close');
						}
					}]
			 });
			//输入
			inputForm = Rainbow.Form({
				  formId:"#inputForm"
				 ,lable:true
				 ,inputArray:[
					 			 {name:"id",type:"hidden"}
					 			,{name:"parent.id",type:"hidden"}
					 			,{lable:"名称",name:"name",option:{required:true}}
					 			,{lable:"编码",name:"code",option:{required:true}}
					 			,{lable:"顺序",name:"sort",type:"numberbox",option:{precision:0,value:1,required:true}}
	 						]
			 	 ,colNum:1
			 });
			 inputDialog = Rainbow.Dialog({
				  title:"字典"
				 ,width:400
				 ,height:300
				 ,onlyLeafCheck:true
				 ,dialogId:'#inputDialog'
				 ,form:inputForm
				 ,serviceName:"DataDictionaryService"
				 ,methodName:"save"
				 ,callback:function(result){
					 var data = result.data;
					 var parentId;
					 if(data.parent){
						 parentId = data.parent.id
					 }
					 var node = grid.treegrid('find',data.id);
					 if(!node){
						 grid.treegrid('append',{
							  parent: parentId
							 ,data:[data]
						 });
						 /* if(parentId){
							 grid.treegrid('refresh',parentId); 
						 } */
					 }else{
						 grid.treegrid('update',{
								 id: data.id
								,row: data
							});
					 }
					 
				 }
			     ,beforeSendData:function(data){
			    	 if(!data.id){
			    		 var row = grid.treegrid("getSelected");
				    	 if(row){
				    		 data.parent = {id:row.id};
				    	 } 
			    	 }
			     }
			 });
			
			//表格
			 grid = new Rainbow.TreeGrid({
				 treeField:"name"
				,queryForm:queryForm
				,serviceName:"DataDictionaryService"
				,methodName:"query"
				,paths:[""]
				,beforeLoad:function(filterOption){
					 filterOption.order=[{sort:1}]
				}
				,columns: [[
					 /* {field:'id',title:'选择'} */
			         {field:'name',title:'名称',width:150}
			        ,{field:'code',title:'编码',width:150}
			        ,{field:'sort',title:'顺序',width:150}
				]]
				,onDblClickRow: function (row) {
					grid.treegrid('select',row.id);
					grid.update(inputDialog,inputForm);
			    }
				,onSave(){
			    	var row = grid.treegrid("getSelected");
			    	var data = {};
			    	var child;
			    	if(row){
			    		child = grid.treegrid("getChildren",row.id);
			    		data.code = row.code+"_";
			    	}else{
			    		child = grid.treegrid("getRoots");
			    	}
			    	if(child && child.length>0){
			    		
		    			data.sort = parseInt(child[child.length-1].sort) + 1;
		    		}else{
		    			data.sort = 1;
		    		}
			    	inputForm.setData(data);
			    }

			});
		});
		</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">
		<div id="toolbar"></div>
		<div id="treeGrid"></div>
		<div id="queryDialog">
			<div id="queryForm"></div>
		</div>
		<div id="inputDialog">
			<form id="inputForm"></form>
		</div>
	</body>
	<script type="text/javascript">
		Rainbow.Util.initButton("#toolbar");
	</script>
	
</html>